package com.ebs.md.fxmonitor.marketmonitor.esper;

/**
/**
* Perforce Revision:
*  $File: //depot/MarketData/fxmonitor/main/src/main/java/com/ebs/md/fxmonitor/marketmonitor/esper/FlashingBasicFactStatement.java $
*  $Revision: #4 $  $Change: 114958 $  $DateTime: 2012/09/13 16:19:35 $  $Author: Arik.Sher $
*/

import java.util.Set;

import com.ebs.md.container.esper.AbsStatement;
import com.ebs.md.fxmonitor.marketmonitor.config.IFlashingConfig;


public class FlashingBasicFactStatement
extends AbsStatement<IFlashingConfig>
{
	protected final static long ESPER_WINDOW_TIME_FRAME_4_FLASHING = 11; // value in seconds
	
	@Override
	protected String createEplExpression() 
	{
		String pairsClauseImplicit = buildEplClauseImplicitPairs();
		
		String str =
			String.format(eplExpressionText
			, config.getName()   	             //1.configuration name.
			, ESPER_WINDOW_TIME_FRAME_4_FLASHING //2. from  OBCRemove.win:ext_timed(timestamp, %dl seconds) as r
			, ESPER_WINDOW_TIME_FRAME_4_FLASHING //3. from  OBCNew   .win:ext_timed(timestamp, %dl seconds) as n
			, getRegion()                        //5. region
			, config.getLifeTimeThreshold()      //5. OBCRemove.entryLifeTime <= %dl
			                                     //6. %d - number of deals.
			, config.getNumberOfInvolvedDealsThreshold() 
			, ESPER_WINDOW_TIME_FRAME_4_FLASHING //7. from Deal.win:ext_timed(timestamp, %dl seconds) as deal
//			, defaultPriceIncrements             //8. pip diffs (price increments)
			, pairsClauseImplicit
			);
		return str;
	}

	protected String buildEplClauseImplicitPairs() {
		StringBuffer sb = new StringBuffer();
		
		Set<String> pairs = config.getPairConfiguredWithPipCountThreshold();
		
		for ( String currPairName : pairs ) {
			appendEplClauseForImplicitPair(currPairName, sb);
		}
		
		return sb.toString();
	}
	
	protected void appendEplClauseForImplicitPair(String shortIsoPairName, StringBuffer sb) {
		int pipCountThreshold = config.getPipCountThreshold(shortIsoPairName);
		
		sb.append( 
			String.format(
				" or ( '%s' = n.pair.shortIsoName and n.pipsDiffFromBest >= %d )"
			  , shortIsoPairName
			  , pipCountThreshold
			)
		);
	}
	
}
